package cn.swust.service.serviceimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.swust.model.DetailProductBean;
import cn.swust.service.DetailProductService;
import cn.swust.util.DBUtil;

public class DetailProductServiceImpl implements DetailProductService{

	public int addDetail(DetailProductBean detail) {
		int ret = 0;
		try{
		String insertSql = "insert into detailproduct(detail_prostate) values ('" + detail.getProState() + "')";
		Connection conn = DBUtil.getConnection();
		Statement state = conn.createStatement();
		state.execute(insertSql, Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = state.getGeneratedKeys();
		while(rs.next())
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return ret;
	}

	public int modDetail(int id, DetailProductBean detail) {
		String modSql = "update detailproduct set detail_prostate = '" +
		detail.getProState() + "' where detail_proid = " + id;
		return DBUtil.updateSql(modSql);
	}

	public DetailProductBean queryById(int id) {
		String querySql = "select * from detailproduct where detail_proid = " + id;
		ResultSet rs = DBUtil.querySql(querySql, DBUtil.creatStatement(DBUtil.getConnection()));
		DetailProductBean de = new DetailProductBean(); 
		try {
			while (rs.next()) {
				de.setProId(rs.getInt("detail_proid"));
				de.setProState(rs.getString("detail_prostate"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return de;
	}

	public int delDetail(int id) {
		String delSql = "delete from detailproduct where detail_proid = " + id;
		return DBUtil.updateSql(delSql);
	}

}
